{% extends 'base.html' %}

{% block title %}组织管理 - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
                <li class="breadcrumb-item active" aria-current="page">组织管理</li>
            </ol>
        </nav>
        
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2><i class="fas fa-building me-2"></i>组织管理</h2>
            <div class="btn-group">
                <a href="{% url 'assets:organization_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>新增组织
                </a>
                <button type="button" class="btn btn-outline-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown">
                    <span class="visually-hidden">更多操作</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="{% url 'assets:bulk_import_organizations' %}">
                        <i class="fas fa-upload me-2"></i>批量导入
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-danger" href="#" onclick="batchDeleteOrganizations()">
                        <i class="fas fa-trash me-2"></i>批量删除
                    </a></li>
                </ul>
            </div>
        </div>
        
        <!-- 搜索和筛选 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-8">
                        <div class="input-group">
                            <input type="text" class="form-control" name="search" placeholder="搜索组织名称或代码..." value="{{ search_query }}">
                            <button class="btn btn-outline-secondary" type="submit">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <button type="button" class="btn btn-outline-info" onclick="clearSearch()">
                            <i class="fas fa-times me-1"></i>清除筛选
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 组织列表 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-list me-2"></i>组织列表
                    {% if page_obj %}
                        <span class="badge bg-secondary ms-2">{{ page_obj.paginator.count }} 个组织</span>
                    {% endif %}
                </h5>
            </div>
            <div class="card-body">
                <form id="batchDeleteForm" method="post" action="{% url 'assets:organization_batch_delete' %}">
                    {% csrf_token %}
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead class="table-dark">
                                <tr>
                                    <th width="5%">
                                        <input type="checkbox" class="form-check-input" id="selectAllHeader" onchange="toggleSelectAll()">
                                    </th>
                                    <th width="20%">组织名称</th>
                                    <th width="15%">组织代码</th>
                                    <th width="35%">描述</th>
                                    <th width="15%">创建时间</th>
                                    <th width="10%">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for organization in page_obj %}
                                <tr>
                                    <td>
                                        <input type="checkbox" class="form-check-input organization-checkbox" name="organization_ids" value="{{ organization.pk }}">
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <i class="fas fa-building me-2 text-primary"></i>
                                            <strong>{{ organization.name }}</strong>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ organization.code }}</span>
                                    </td>
                                    <td>
                                        {% if organization.description %}
                                            <span title="{{ organization.description }}">{{ organization.description|truncatechars:50 }}</span>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="text-nowrap">
                                            <i class="fas fa-calendar me-1 text-muted"></i>
                                            {{ organization.created_at|date:"m-d H:i" }}
                                        </div>
                                        <small class="text-muted">{{ organization.created_at|date:"Y" }}</small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'assets:organization_detail' organization.pk %}" class="btn btn-outline-info" title="查看详情">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'assets:organization_update' organization.pk %}" class="btn btn-outline-primary" title="编辑">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="{% url 'assets:organization_delete' organization.pk %}" class="btn btn-outline-danger" title="删除" onclick="return confirm('确定要删除组织 {{ organization.name }} 吗？')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="6" class="text-center py-4">
                                        <div class="text-muted">
                                            <i class="fas fa-building fa-2x mb-2"></i>
                                            <p class="mb-0">暂无组织数据</p>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </form>
            </div>
        </div>

        <!-- 分页 -->
        {% if page_obj.has_other_pages %}
        <nav aria-label="页面导航" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                {% endif %}
                
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}">{{ num }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
                
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<script>
function toggleSelectAll() {
    const selectAllCheckbox = document.getElementById('selectAllHeader');
    const organizationCheckboxes = document.querySelectorAll('.organization-checkbox');
    const isChecked = selectAllCheckbox.checked;
    
    organizationCheckboxes.forEach(checkbox => {
        checkbox.checked = isChecked;
    });
}

function batchDeleteOrganizations() {
    const checkedBoxes = document.querySelectorAll('.organization-checkbox:checked');
    
    if (checkedBoxes.length === 0) {
        alert('请选择要删除的组织');
        return;
    }
    
    const organizationNames = Array.from(checkedBoxes).map(checkbox => {
        const row = checkbox.closest('tr');
        return row.querySelector('strong').textContent;
    });
    
    if (confirm(`确定要删除以下 ${checkedBoxes.length} 个组织吗？\n\n${organizationNames.join(', ')}\n\n此操作不可撤销！`)) {
        document.getElementById('batchDeleteForm').submit();
    }
}

function clearSearch() {
    window.location.href = '{% url "assets:organization_list" %}';
}

// 监听单个复选框变化，更新全选状态
document.addEventListener('DOMContentLoaded', function() {
    const organizationCheckboxes = document.querySelectorAll('.organization-checkbox');
    const selectAllHeader = document.getElementById('selectAllHeader');
    
    organizationCheckboxes.forEach(checkbox => {
        checkbox.addEventListener('change', function() {
            const checkedCount = document.querySelectorAll('.organization-checkbox:checked').length;
            const totalCount = organizationCheckboxes.length;
            
            if (selectAllHeader) selectAllHeader.checked = checkedCount === totalCount;
        });
    });
});
</script>
{% endblock %}